package com.lz.football.module.shop
{
	import com.common.utils.Reflection;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.constants.TipsConst;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.liangzhong.model.shop.vo.ShopVo;
	import com.liangzhong.model.type.GoodsType;
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.GoodItem;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.managers.TipsManager;
	
	public class ShopItem extends Sprite implements IComponent
	{
		/**
		 * 
		 */
		private var _skin:MovieClip;
		
		private var _goodsVo:GoodsVo;
		/**
		 *物品名称 
		 */
		private var goodsName:TextField;
		/**
		 *费用  
		 */
		private var cost:TextField;
		/**
		 *条件 
		 */
		private var condition:TextField;
		/**
		 * 购买按钮 
		 */
		private var buyBtn:SimpleButton;
		/**
		 * 
		 */
		private var _bitmap:Bitmap;
		/**
		 *物品背景 
		 */
		private var _goodItem:GoodItem;
		/**
		 * 物品背景
		 */
		private var bitBg:MovieClip;
		/**
		 *背景 
		 */
		private var itemBg:MovieClip;
		/**
		 *购买国家 
		 */
		private var _buyCountry:String;
		private var _callBack:Function;
//		/**
//		 *球员背景 
//		 */
//		private var playerBg:MovieClip;
		public function ShopItem()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = Reflection.createInstance("com.lz.modul.shop.item");
			this.addChild(_skin);
			goodsName = _skin.goodsName;
			cost = _skin.cost;
			condition = _skin.condition;
			goodsName.mouseEnabled = false;
			cost.mouseEnabled = false;
			condition.mouseEnabled = false;
//			playerBg = Reflection.createInstance("com.lz.res.PlayerColorBg");
//			playerBg.x = 5;
//			playerBg.y = 24;
//			playerBg.mouseChildren = false;
//			playerBg.mouseEnabled = false;
			buyBtn = _skin.buyBtn;
			bitBg = _skin.bitBg;
			itemBg = _skin.itemBg;
			bitBg.gotoAndStop(1);
			_bitmap = new Bitmap();
			_goodItem = new GoodItem();
			_goodItem.initUI();
//			this.addChild(_bitmap);
			addEvtListener();
		}
		
		
		public function setData($vo:ShopVo):void
		{
			reset();
			_goodsVo = AppData.getInstance().goodsManager.getGoodsByTypeAndId($vo.ntype,$vo.id);
			if(_goodsVo == null)return;
			goodsName.text = _goodsVo.name;
			cost.text = _goodsVo.cashpoint + "";
//			_bitmap.bitmapData = null;
//			_bitmap.bitmapData = _goodsVo.bitmapData;
//			_bitmap.x = 6
//			_bitmap.y = 23;
			
			_goodItem.data = _goodsVo;
			_goodItem.x = 3
			_goodItem.y = 21;
			if($vo.country > 0 && _goodsVo.type ==  GoodsType.PLAYERCARD)
			{
				if($vo.country == AppData.getInstance().clubDatamanager.clubInfo.country)
				{
					condition.textColor = 0x00FF00
				}else
				{
					condition.textColor = 0xFF0000
				}
				_buyCountry = ClubConst.countryToString($vo.country)
				condition.text = "条件："+_buyCountry;
				itemBg.addEventListener(MouseEvent.ROLL_OUT,onOutHandler);
				itemBg.addEventListener(MouseEvent.ROLL_OVER,onItemOverHandler);
				bitBg.gotoAndStop((_goodsVo as PlayerStarVo).color);
				
			}
//			if(_goodsVo.type == GoodsType.PLAYERCARD)
//			{
//				this.addChild(playerBg);
//			}
			this.addChild(_goodItem);
		}
		protected function onItemOverHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			TipsManager.show(TipsConst.BASETIPS,{tip:"购买国家：" +_buyCountry},15,15,false);
		}
		
		public function reset():void
		{
			bitBg.gotoAndStop(1);
			goodsName.text = "";
			cost.text = "";
			condition.text = "";
			_goodsVo = null;
			_goodItem.data = null;
			itemBg.removeEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			itemBg.removeEventListener(MouseEvent.ROLL_OVER,onItemOverHandler);
//			if(playerBg.parent)
//				playerBg.parent.removeChild(playerBg);
		}
		public function addEvtListener():void
		{
			bitBg.addEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			bitBg.addEventListener(MouseEvent.ROLL_OVER,onBitOverHandler);
			buyBtn.addEventListener(MouseEvent.CLICK,onBuyHandler);
		}
		
		/**
		 *购买 
		 * @param event
		 * 
		 */
		protected function onBuyHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_goodsVo == null)return;
			if(_goodsVo.type == GoodsType.PLAYERCARD && _buyCountry != AppData.getInstance().clubDatamanager.clubInfo.countryToStr)
			{
				Alert.showErrorTip("国籍不同，不可购买");
				return;
			}
			if(_callBack != null)
				_callBack(_goodsVo);
		}
		
		protected function onBitOverHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_goodsVo == null)return;
			if(_goodsVo.type == GoodsType.PLAYERCARD)
			{
				TipsManager.show(TipsConst.PLAYERCARDTIPS,_goodsVo,15,15,false);
			}
			else
			{
				TipsManager.show(TipsConst.BASETIPS,{tip:_goodsVo.tipsInfo},15,15,false);
			}
		}
		
		protected function onOutHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			TipsManager.hide();
		}
		
		public function removeEvtListener():void
		{
			itemBg.removeEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			itemBg.removeEventListener(MouseEvent.ROLL_OVER,onItemOverHandler);
			bitBg.removeEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			bitBg.removeEventListener(MouseEvent.ROLL_OVER,onBitOverHandler);
			buyBtn.removeEventListener(MouseEvent.CLICK,onBuyHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			_callBack = null;
			if(this.parent)
				this.parent.removeChild(this);
			_skin = null;
			
		}

		/**
		 *回调函数 
		 */
		public function get callBack():Function
		{
			return _callBack;
		}

		/**
		 * @private
		 */
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}

	}
}